home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / Debian / DocBase / InstallDocs.pm < prev    next >
Encoding:
Perl POD Document  |  2008-09-07  |  7.6 KB  |  269 lines

  1. #!/usr/bin/perl
  2.  
  3. # vim:cindent:ts=2:sw=2:et:fdm=marker:cms=\ #\ %s
  4. #
  5. # $Id: InstallDocs.pm 147 2008-05-13 19:42:14Z robert $
  6.  
  7. package Debian::DocBase::InstallDocs;
  8.  
  9. use warnings;
  10. use strict;
  11.  
  12. use base qw(Exporter);
  13. use vars qw(@EXPORT);
  14. our @EXPORT = qw(SetMode InstallDocsMain
  15.                  $MODE_INSTALL $MODE_REMOVE $MODE_STATUS $MODE_REMOVE_ALL $MODE_INSTALL_ALL
  16.                  $MODE_INSTALL_CHANGED $MODE_DUMP_DB $MODE_CHECK  $verbose $debug);
  17.  
  18. use Carp;
  19. use Debian::DocBase::Common;
  20. use Debian::DocBase::Utils;
  21. use Debian::DocBase::Document;
  22. use Debian::DocBase::DocBaseFile;
  23. use Debian::DocBase::DB;
  24. use Debian::DocBase::Programs::Dhelp;
  25. use Debian::DocBase::Programs::Dwww;
  26. use Debian::DocBase::Programs::Scrollkeeper;
  27. use File::Path;
  28.  
  29.  
  30. # constants
  31. our $MODE_INSTALL         = 1;
  32. our $MODE_REMOVE          = 2;
  33. our $MODE_INSTALL_ALL     = 3;
  34. our $MODE_REMOVE_ALL      = 4;
  35. our $MODE_STATUS          = 5;
  36. our $MODE_CHECK           = 6;
  37. our $MODE_INSTALL_CHANGED = 7;
  38. our $MODE_DUMP_DB         = 8;
  39.  
  40. # global module variables
  41. our $mode                 = undef;
  42. our @arguments            = undef;
  43.  
  44. #################################################
  45. ###        PUBLIC STATIC FUNCTIONS            ###
  46. #################################################
  47.  
  48. # Sets work mode
  49. sub SetMode($@) { # {{{
  50.   my $newmode = shift;
  51.   my @args    = @_;
  52.  
  53.  
  54.   croak("Internal error: mode already set: $mode, $newmode") if (defined $mode);
  55.  
  56.   $mode = $newmode;
  57.  
  58.   Inform("Value of --rootdir option ignored") if ($mode != $MODE_CHECK) and ($opt_rootdir ne "");
  59.  
  60.   if ($#args == 0 and $args[0] eq '-') {
  61.     # get list from stdin
  62.     @arguments = map {+chomp} <STDIN>;
  63.   }
  64.   else {
  65.     @arguments = @args;
  66.   }
  67.  
  68. } # }}}
  69.  
  70. # Main procedure that gets called by install-docs
  71. sub InstallDocsMain() { # {{{
  72.  
  73.   croak("Internal error: Unknown mode") unless defined $mode;
  74.  
  75.   if ($mode == $MODE_CHECK) {
  76.     _HandleCheck();
  77.   } elsif ($mode == $MODE_STATUS) {
  78.     _HandleStatus();
  79.   } elsif ($mode == $MODE_DUMP_DB) {
  80.     _HandleDumpDB();
  81.   } elsif ($mode == $MODE_REMOVE_ALL) {
  82.     _HandleRemovalOfAllDocs();
  83.   } else {
  84.     _HandleRegistrationAndUnregistation();
  85.   }
  86.  
  87.   # don't fail on reregistering docs
  88.   $exitval = 0 if    $mode == $MODE_INSTALL_ALL
  89.                   or $mode == $MODE_REMOVE_ALL
  90.                   or $mode == $MODE_INSTALL_CHANGED;
  91.  
  92. } # }}}
  93.  
  94. #################################################
  95. ###        PRIVATE STATIC FUNCTIONS           ###
  96. #################################################
  97.  
  98. # Check correctness of doc-base file
  99. sub _HandleCheck() { # {{{
  100.   foreach my $file (@arguments) {
  101.     if (! -f $file) {
  102.       Error("Can't read doc-base file `$file'");
  103.       next;
  104.     }
  105.  
  106.     my $docfile = Debian::DocBase::DocBaseFile->new($file, 1);
  107.     $docfile->Parse();
  108.     if ($docfile->Invalid()) {
  109.         Inform("$file: Fatal error found, the file won't be registered");
  110.     } elsif ((my $cnt = $docfile->GetWarnErrCount()) > 0) {
  111.         Inform("$file: $cnt warning(s) or non-fatal error(s) found");
  112.     } else {
  113.         Inform("$file: No problems found");
  114.     }
  115.   }
  116. } # }}}
  117.  
  118. # Show document status
  119. sub _HandleStatus() { # {{{
  120.   foreach my $docid (@arguments) {
  121.     unless (Debian::DocBase::Document::IsRegistered($docid)) {
  122.       Inform ("Document `$docid' is not registered");
  123.       next;
  124.     }
  125.     my $doc = Debian::DocBase::Document->new($docid);
  126.     $doc -> DisplayStatusInformation();
  127.   }
  128. } # }}}
  129.  
  130. # Dump our databases
  131. sub _HandleDumpDB() { # {{{
  132.   foreach my $arg (@arguments) {
  133.     if ($arg eq "files.db") {
  134.       Debian::DocBase::DB::GetFilesDB()->DumpDB();
  135.     } elsif ($arg eq "status.db") {
  136.       Debian::DocBase::DB::GetStatusDB()->DumpDB();
  137.     } else {
  138.       Error("Invalid argument `$arg' passed to --dump-db option");
  139.       exit (1);
  140.     }
  141.   }
  142. } # }}}
  143.  
  144. # Remove all docs simply by deleting our db and other created files
  145. sub _HandleRemovalOfAllDocs() { # {{{
  146.   my $suffix  = ".removed.$$";
  147.   my @dbdirs  = ($OMF_DIR, $VAR_CTRL_DIR);
  148.  
  149.   unlink $DB_FILES or croak("Can't remove $DB_FILES: $!") if -f $DB_FILES;
  150.   foreach my $d (@dbdirs) {
  151.     next unless -d $d;
  152.     rename ($d, $d.$suffix) or croak("Can't rename $d to ${d}${suffix}: $!");
  153.     mkpath ($d, 0, 0755);
  154.     rmtree ($d.$suffix, 0, 0);
  155.   }
  156.   unlink $DB_STATUS or croak("Can't remove $DB_STATUS: $!") if -f $DB_STATUS;
  157.  
  158.   my @documents = ();
  159.   RegisterDwww(1, @documents);
  160.   RegisterDhelp(1, 1, @documents);
  161.   RegisterScrollkeeper(1, @documents);
  162.  
  163. } # }}}
  164.  
  165. # Register or de-register particular docs or register all or only changed docs
  166. sub _HandleRegistrationAndUnregistation() { # {{{
  167.   my @toinstall     = ();       # list of files to install
  168.   my @toremove      = ();       # list of files to remove
  169.   my @toremovedocs  = ();       # list of docs to remove
  170.   my $msg           = "";
  171.  
  172.   if ($mode == $MODE_INSTALL_CHANGED) {
  173.     my @stats = Debian::DocBase::DocBaseFile::GetChangedDocBaseFiles(\@toremove, \@toinstall);
  174.  
  175.     my $i      = 0;
  176.     $msg      .= ($i++ ? ""   : "") . $stats[0] .  " removed" if $stats[0];
  177.     $msg      .= ($i++ ? ", " : "") . $stats[1] .  " changed" if $stats[1];
  178.     $msg      .= ($i++ ? ", " : "") . $stats[2] .  " added"   if $stats[2];
  179.     Inform("Processing $msg doc-base file(s)...") if $msg;
  180.   }
  181.  
  182.   elsif ($mode == $MODE_INSTALL_ALL) {
  183.     @toremovedocs  = Debian::DocBase::Document::GetAllRegisteredDocumentIDs();
  184.     @toinstall     = Debian::DocBase::DocBaseFile::GetAllDocBaseFiles() if $mode == $MODE_INSTALL_ALL;
  185.     my @stats      = ($#toremovedocs+1, $#toinstall+1);
  186.     my $i          = 0;
  187.     $msg          .=  ($i++ ? "" : "")  .  "De-registering "       . $stats[0] if $stats[0];
  188.     $msg          .=  ($i++ ? ", re-registering "  : "Registering ") . $stats[1] if $stats[1];
  189.     Inform("$msg doc-base file(s)...") if $msg;
  190.   }
  191.  
  192.   elsif  ($mode == $MODE_INSTALL) {
  193.     @toinstall = @arguments;
  194.   }
  195.  
  196.   elsif ($mode == $MODE_REMOVE)  {
  197.     @toremove     = grep { /\//  } @arguments;
  198.     @toremovedocs = grep { /^[^\/]+$/ } @arguments; # for backward compatibility  -> arguments are document-ids
  199.  
  200.   }
  201.  
  202.   foreach my $docid (@toremovedocs) {
  203.     unless (Debian::DocBase::Document::IsRegistered($docid)) {
  204.       Inform ("Ignoring nonregistered document `$docid'");
  205.       next;
  206.     }
  207.     Debug("Trying to remove document $docid");
  208.     my $doc   = Debian::DocBase::Document->new($docid);
  209.     $doc->UnregisterAll();
  210.   }
  211.  
  212.   foreach my $file (@toremove) {
  213.     my $docid   = Debian::DocBase::DocBaseFile::GetDocIdFromRegisteredFile($file);
  214.     unless ($docid) {
  215.       Inform ("Ignoring nonregistered file `$file'");
  216.       next;
  217.     }
  218.     my $docfile = Debian::DocBase::DocBaseFile->new($file);
  219.     my $doc     = Debian::DocBase::Document->new($docid);
  220.     $doc->Unregister($docfile);
  221.   }
  222.  
  223.   foreach my $file (@toinstall) {
  224.     unless (-f $file) {
  225.       Error("Can't read doc-base file `$file'");
  226.       next;
  227.     }
  228.     Debug("Trying to install file $file");
  229.     my $docfile = Debian::DocBase::DocBaseFile->new($file,  $opt_verbose);
  230.     $docfile->Parse();
  231.     my $docid   = $docfile->GetDocumentID();
  232.     next unless defined $docid;
  233.     my $doc     = Debian::DocBase::Document->new($docid);
  234.  
  235.     $doc->Register($docfile);
  236.   }
  237.  
  238.   my @documents = Debian::DocBase::Document::GetDocumentList();
  239.  
  240.   UnregisterDhelp(@documents) if @documents and $mode != $MODE_INSTALL_ALL;
  241.  
  242.   foreach my $doc (@documents) {
  243.       $doc -> MergeCtrlFiles();
  244.   }
  245.  
  246.   IgnoreSignals();
  247.   foreach my $doc (@documents) {
  248.     $doc -> WriteNewCtrlFile();
  249.     $doc -> SaveStatusChanges();
  250.   }
  251.   RestoreSignals();
  252.  
  253.   if (@documents)
  254.   {
  255.     my $showmsg = ($opt_verbose or $msg);
  256.  
  257.     RegisterDwww($showmsg,          @documents);
  258.     RegisterDhelp($showmsg,         $mode == $MODE_INSTALL_ALL, @documents);
  259.     RegisterScrollkeeper($showmsg,  @documents);
  260.   }
  261.  
  262.   undef @toinstall;
  263.   undef @toremove;
  264.   undef @toremovedocs;
  265.  
  266. } # }}}
  267.  
  268. 1;
  269.